const express = require('express')
const app = express()
const port = 2323

app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
});

// 健康度排序接口
app.get('/health_data', (req, res) => {
    const ret = {
        "name": 'ym',
        "age": 22,
        "data": [
            { "name": "十堰市", "score": 50 },
            { "name": "襄阳市", "score": 46 },
            { "name": "荆门市", "score": 78 },
            { "name": "荆州市", "score": 98 },
            { "name": "潜江市", "score": 68 },
            { "name": "孝感市", "score": 49 },
            { "name": "天门市", "score": 68 },
            { "name": "仙桃市", "score": 73 },
            { "name": "武汉市", "score": 85 },
            { "name": "咸宁市", "score": 91 },
            { "name": "黄冈市", "score": 74 },
            { "name": "黄石市", "score": 68 },
            { "name": "鄂州市", "score": 97 },
            { "name": "恩施", "score": 38 },
            { "name": "神农架", "score": 58 }
        ]
    }
    res.send(ret)
})

// 实时健康度接口
app.get('/holographic_data', (req, res) => {
    res.send({
        "name": 'ym',
        "age": 22,
        "data": [
            { "time": "00:00", "score": 42 },
            { "time": "01:00", "score": 54 },
            { "time": "02:00", "score": 56 },
            { "time": "03:00", "score": 62 },
            { "time": "04:00", "score": 63 },
            { "time": "05:00", "score": 66 },
            { "time": "06:00", "score": 69 },
            { "time": "07:00", "score": 73 },
            { "time": "08:00", "score": 77 },
            { "time": "09:00", "score": 70 },
            { "time": "10:00", "score": 80 },
            { "time": "11:00", "score": 83 },
            { "time": "12:00", "score": 42 },
            { "time": "13:00", "score": 54 },
            { "time": "14:00", "score": 56 },
            { "time": "15:00", "score": 62 },
            { "time": "16:00", "score": 63 },
            { "time": "17:00", "score": 66 },
            { "time": "18:00", "score": 69 },
            { "time": "19:00", "score": 73 },
            { "time": "20:00", "score": 77 },
            { "time": "21:00", "score": 70 },
            { "time": "22:00", "score": 80 },
            { "time": "23:00", "score": 83 },
        ]
    })
})

// 当前分层健康度
app.get('/layerd_now_data', (req, res) => {
    res.send({
        name: "ym",
        age: 23,
        data: [
            { "name": "基础设施", "score": 56 },
            { "name": "服务资源", "score": 60 },
            { "name": "核心数据", "score": 71 },
            { "name": "采集设备", "score": 80 },
            { "name": "应用平台", "score": 23 },
        ]
    })
})

// 历史分层健康度
app.get('/layerd_old_data', (req, res) => {
    var category_by = req.query.category_by
    let sendData = {}
    const monthData = {
        "category_by": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        "data": [
            {
                "name": "基础设施",
                "data": [20, 25, 27, 30, 34, 39, 45, 39, 33, 35, 30]
            },
            {
                "name": "采集设备",
                "data": [26, 31, 33, 36, 40, 45, 51, 45, 39, 41, 36]
            },
            {
                "name": "服务资源",
                "data": [31, 36, 38, 40, 47, 50, 52, 49, 45, 48, 42]
            },
            {
                "name": "核心数据",
                "data": [42, 46, 48, 54, 59, 61, 66, 60, 56, 55, 45]
            },
            {
                "name": "应用平台",
                "data": [50, 56, 57, 65, 68, 72, 79, 70, 64, 68, 60]
            },
        ]
    }
    const dayData = {
        "category_by": ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00",
            "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"],
        "data": [
            {
                "name": "基础设施",
                "data": [20, 25, 27, 30, 34, 39, 45, 39, 33, 35, 30, 20, 25, 27, 30, 34, 39, 45, 39, 33, 35, 30]
            },
            {
                "name": "采集设备",
                "data": [25, 30, 32, 35, 39, 44, 49, 44, 38, 41, 36, 25, 30, 32, 35, 39, 44, 49, 54, 38, 41, 36]
            },
            {
                "name": "服务资源",
                "data": [30, 35, 37, 40, 44, 49, 54, 49, 43, 46, 41, 30, 35, 37, 40, 44, 49, 54, 59, 43, 46, 41]
            },
            {
                "name": "核心数据",
                "data": [35, 40, 42, 45, 49, 54, 59, 54, 48, 51, 46, 35, 40, 42, 45, 49, 54, 59, 54, 48, 51, 46]
            },
            {
                "name": "应用平台",
                "data": [40, 45, 47, 50, 54, 59, 64, 59, 53, 55, 51, 40, 45, 47, 50, 54, 59, 64, 59, 53, 55, 51]
            },
        ]
    }
    switch (category_by) {
        case 'day':
            sendData = dayData
            break
        case 'week':
            sendData = weekData
            break
        case 'month':
            sendData = monthData
            break
        case 'quarter':
            sendData = quarterData
            break
        case 'year':
            sendData = yearData
            break
    }
    res.send(sendData)
})

// 构造地图数据
app.get('/map_data', (req, res) => {
    res.send({
        name: 'ym',
        age: 22,
        data: [
            { name: "十堰市", value: 50 },
            { name: "神农架林区", value: 58 },
            { name: "恩施土家族苗族自治州", value: 40 },
            { name: "宜昌市", value: 28 },
            { name: "襄阳市", value: 89 },
            { name: "荆门市", value: 79 },
            { name: "荆州市", value: 83 },
            { name: "随州市", value: 91 },
            { name: "天门市", value: 86 },
            { name: "潜江市", value: 76 },
            { name: "仙桃市", value: 63 },
            { name: "孝感市", value: 78 },
            { name: "武汉市", value: 85 },
            { name: "咸宁市", value: 96 },
            { name: "黄冈市", value: 37 },
            { name: "鄂州市", value: 45 },
            { name: "黄石市", value: 99 },
        ],
    })
})

// 告警数据
app.get('/alarm_data', (req, res) => {
    res.send({
        name: 'ym',
        data: [
            {
                time: "2016-05-02",
                device: "中间件",
                threshold: 60,
                health_score: 50,
                platform: "联网平台",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "视频流采集设备",
                threshold: 60,
                health_score: 50,
                platform: "联网平台",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "计算设备",
                threshold: 60,
                health_score: 50,
                platform: "联网平台",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "存储设备",
                threshold: 60,
                health_score: 50,
                platform: "解析系统",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "计算设备",
                threshold: 60,
                health_score: 50,
                platform: "解析系统",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "计算设备",
                threshold: 60,
                health_score: 50,
                platform: "综合应用平台",
            },
            {
                time: "2016-05-02 12:12:12",
                device: "计算设备",
                threshold: 60,
                health_score: 50,
                platform: "安全管控系统",
            },
        ]
    })
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})